package top.bingoz.wanandroid.base

import android.arch.lifecycle.Lifecycle
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.KeyCharacterMap
import android.view.KeyEvent
import android.view.View
import android.view.ViewConfiguration
import com.trello.lifecycle2.android.lifecycle.AndroidLifecycle
import com.trello.rxlifecycle2.LifecycleProvider
import top.bingoz.wanandroid.contract.Contract
import top.bingoz.wanandroid.toast

/**
 * @author su
 * @date 2018/7/29
 * @description 基类
 */
abstract class BaseActivity : AppCompatActivity(), Contract.View {
    protected val provider: LifecycleProvider<Lifecycle.Event> = AndroidLifecycle.createLifecycleProvider(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())
        window.decorView.findViewById<View>(android.R.id.content).setPadding(0, 0, 0, getNavigationBarHeight())

        initialize()

    }

    fun getNavigationBarHeight(): Int {
        val hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey()
        val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
        if (!hasMenuKey && !hasBackKey) {
            val resources = resources
            val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            //获取NavigationBar的高度
            return resources.getDimensionPixelSize(resourceId)
        } else {
            return 0
        }

    }

    open protected fun initialize() {
    }

    abstract fun getLayoutId(): Int

    override fun showloading() {
    }

    override fun dismissloading() {

    }

    override fun showError(msg: String?) {
        toast(msg)
    }

}